/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-ice
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Jul 1, 2011 3:07:08 PM
 *
*/ 
package org.justframework.ext.ice;

import org.justframework.core.Request;
import org.justframework.core.RequestHandler;
import org.justframework.core.Response;
import org.justframework.core.support.JustRequestHandler;
import org.justframework.core.support.RequestHandlerFactory;
import org.justframework.ext.ice.zeroc._ZerocRpcDisp;
import org.justframework.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import Ice.Current;

/**
 * @author whx
 *
 */
public class IceHandlerAdapter extends _ZerocRpcDisp{
	
	/**
	 * logger
	 */
	private static Logger logger = LoggerFactory.getLogger(IceHandlerAdapter.class);

	/* (non-Javadoc)
	 * @see org.justframework.ext.ice.zeroc._ZerocRpcOperations#rpc(java.lang.String, Ice.Current)
	 */
	public String rpc(String requestStr, Current __current) {
		Request request = getRequest(requestStr);
		logger.debug("request obj:{}", request);
		Response response = new Response();
		RequestHandler handler = RequestHandlerFactory.getRequestHandler();
		handler.handle(request, response, null);
		logger.debug("response obj:{}",response);
		String result = JsonUtil.toJson(response);
		return result;
	}
	
	private Request getRequest(String requestStr){
		Request request = JsonUtil.toBean(requestStr, Request.class);
		return request;
	}

}
